package io.github.talelin.latticy.controller.v1;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.core.annotation.GroupRequired;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.core.annotation.PermissionMeta;
import io.github.talelin.core.annotation.PermissionModule;
import io.github.talelin.latticy.common.mybatis.Page;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.dto.CouponDTO;
import io.github.talelin.latticy.dto.CouponTemplateDTO;
import io.github.talelin.latticy.dto.query.BasePageDTO;
import io.github.talelin.latticy.model.CouponTemplate;
import io.github.talelin.latticy.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.github.talelin.latticy.model.Coupon;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;

import javax.validation.constraints.Min;
import javax.validation.constraints.Max;
import javax.validation.constraints.Positive;
import java.util.List;

/**
* @author tanrentao
* @since 2021-08-11
*/
@Validated
@RestController
@RequestMapping("/v1/coupon")
@PermissionModule("优惠券")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @GroupRequired
    @PostMapping("")
    @PermissionMeta("创建优惠券")
    public CreatedVO create(@RequestBody @Validated CouponDTO dto) {
        couponService.create(dto);
        return new CreatedVO();
    }

    @GroupRequired
    @PutMapping("/{id}")
    @PermissionMeta("更新优惠券")
    public UpdatedVO update(@RequestBody @Validated CouponDTO dto,
                            @PathVariable @Positive(message = "{id}") Integer id) {
        couponService.update(dto, id);
        return new UpdatedVO();
    }

    @GroupRequired
    @DeleteMapping("/{id}")
    @PermissionMeta("删除优惠券")
    public DeletedVO delete(@PathVariable @Positive(message = "{id}") Integer id) {
        couponService.delete(id);
        return new DeletedVO();
    }

    @LoginRequired
    @GetMapping("/{id}")
    @PermissionMeta("根据id查询优惠券")
    public Coupon get(@PathVariable(value = "id") @Positive(message = "{id}") Integer id) {
        Coupon coupon = couponService.getById(id);
        if (coupon == null) {
            throw new NotFoundException(100000);
        }
        return coupon;
    }

    @LoginRequired
    @GetMapping("/page")
    @PermissionMeta("分页查询优惠券")
    public PageResponseVO<Coupon> page(@RequestBody @Validated BasePageDTO dto) {
        Page<Coupon> page = new Page<>(dto.getPage(), dto.getCount());
        IPage<Coupon> paging = couponService.getBaseMapper().selectPage(page, null);
        return PageUtil.build(paging);
    }

    @GroupRequired
    @PostMapping("/template")
    @PermissionMeta("创建优惠券模板")
    public CreatedVO createTemplate(@RequestBody @Validated CouponTemplateDTO dto) {
        couponService.createTemplate(dto);
        return new CreatedVO();
    }

    @GroupRequired
    @PutMapping("/template/{id}")
    @PermissionMeta("更新优惠券模板")
    public UpdatedVO updateTemplate(@RequestBody @Validated CouponTemplateDTO dto,
                                    @PathVariable @Positive(message = "{id}") Integer id) {
        couponService.updateTemplate(dto, id);
        return new UpdatedVO();
    }

    @LoginRequired
    @GetMapping("/template/{id}")
    @PermissionMeta("根据id获取优惠券模板")
    public CouponTemplate getTemplate(@PathVariable @Positive(message = "{id}") Integer id) {
        return couponService.getTemplate(id);
    }

    @LoginRequired
    @DeleteMapping("/template/{id}")
    @PermissionMeta("根据id删除优惠券模板")
    public DeletedVO deleteTemplate(@PathVariable @Positive(message = "{id}") Integer id) {
        couponService.deleteTemplate(id);
        return new DeletedVO();
    }

    @LoginRequired
    @GetMapping("/templates")
    @PermissionMeta("获取优惠券模板列表")
    public List<CouponTemplate> templates() {
        List<CouponTemplate> templates = couponService.getTemplates();
        return templates;
    }

    @LoginRequired
    @GetMapping("/list")
    @PermissionMeta("根据id获取活动列表")
    public List<Coupon> getListByActivityId(@RequestParam(name = "id") @Min(value = 1, message = "{id}") Integer id) {
        List<Coupon> coupons = couponService.getListByActivityId(id);
        return coupons;
    }

}
